const app = getApp()
Page({
  data: {
    showPayment:false,
    overlay:false,
    times:false,
    showShare:false,
    onPayBtn:'立即购买',
    id: 0,
    shop:{},
    store:{},
    coupon:{},
    share:[],
    order:[],
    painting: {},
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onLoad: function (options) {
    app.getParam(options,(rel) => {
      this.data.id =  rel.id
      this.getItem();
    });
  }, 
  /**
   * 获取商品信息
   */
  getItem: function () {
    app.api().Get('api/v1/ais/shop/item',{id: this.data.id},(result) => {
      this.setData({
        shop: result.data,
        store: result.data.store
      });
      if(result.data.coupon_id > 0){
        app.api().Get('api/v1/ais/coupon/read',{id: result.data.coupon_id},(result) => {
          if (result.code == 200) {
            this.setData({
              coupon: result.data,
            });
          }
        })
      }
    })
    //增加分享用户
    app.api().Get('api/v1/ais/shop/orderUser',{id: this.data.id},(result) => {
      if(result.code == 200){
        this.setData({
          share: result.data.share,
        });
        setTimeout(()=>{
          this.setData({
            times: result.data.order.create_time <= 0 ? false : true,
            order: result.data.order,
          });
        },1200)
      }
    })
  },
   //购买
  showPayment:function () {
    this.setData({
      showPayment:!this.data.showPayment,
      onPayBtn:this.data.showPayment ? '立即购买':'同意并下单'
    })
  },
  onPayment:function () {
    this.showPayment();
    wx.navigateTo({
      url:'cart?id='+this.data.id
    })
  },
  //重新支付
  restOrder:function () {
    this.onHideOrder()
    wx.showLoading({
      title:'加载中'
    })
    app.api().Post('api/v1/ais/shoppay/payment',{id: this.data.id},(res) => {
      if (res.code == 200) {
       wx.hideLoading()
       app.doWechatPay(res.data,()=>{
          wx.navigateTo({
            url: 'user/index?active=2',
          })
       },()=>{
        app.wxAlert('你取消了支付')
       })
      }
    })
  },
  //预览订单
  viewOrder:function () {
    wx.navigateTo({
      url: 'user/view?id='+this.data.order.id,
    })
  },
  //分享按钮
  onShareAppMessage: function () {
    this.setData({
      showShare:false
    });
    return {
      title: this.data.shop.name,
      desc: this.data.shop.title,
      path: '/packageIndex/pages/shop/item?id='+this.data.shop.id+'&ucode='+app.globalData.loginuser.ucode,
    }
  },
  //加群
  onClickHide() {
    this.setData({
      overlay: !this.data.overlay
    });
  },
  //下单倒计时
  onHideOrder() {
    this.setData({
      times:false
    });
  },
  //分享
  onHideShare() {
    this.setData({
      showShare: !this.data.showShare
    });
    if(this.data.showShare){
      this.createKv();
    }
  },
  //改变时间
  onChange(e) {
    this.setData({
      timeData: e.detail
    });
  },
  //结束倒计时
  finished(e) {
    var shop = this.data.shop;
        shop.end_time = 0;
    this.setData({
      shop:shop
    });
  },
  //图片比例显示
  imageLoad: function(e) {
    var width= e.detail.width,height= e.detail.height;
    var viewWidth = 500,viewHeight = 500/(width/height);
    this.setData({
      ['shareCode.width']: viewWidth,
      ['shareCode.height']:viewHeight
    })
  },
  //创建分享海报
  createKv: function (e) {
    var that = this;
    wx.showLoading({
      title:'正在生成海报',mask: true
    })
    app.api().Post('openapi/v1/user/MiniProgramQrCode', {
      scene: {
        id:this.data.id,
        ucode:app.globalData.ucode
      },
      page: 'pages/shop/item',
      name: 'ais_shop_share_'+app.globalData.ucode+that.data.shop.id
    },(result)=>{
      var paintingList = {
        width: 750,
        height:1100,
        clear: true,
        views: [
          {type:'image',url: '/packageIndex/img/share.jpg',top: 0,left: 0,width: 750,height:1100},
          {type:'image',url:this.data.shop.img,top:62,left:37,width:680,height:680},
          {type:'text',content:this.data.shop.name,breakWord:true,fontSize:32,lineHeight:45,width:650,top:770,left:50},
          {type:'text',content:'￥'+this.data.shop.sell_price,color:"#FF0000",breakWord:true,fontSize:45,top:888,left:150},
          {type:'image',url:result.data,top:860,left:490,width:198,height:230}
      ]};
      that.setData({
        painting:paintingList
      })
    })
  },
  eventGetImage: function (e) {
    this.setData({
      shareImage:e.detail.tempFilePath
    })
    wx.hideLoading()
  },
  //保存分享海报
  saveKv: function () {
    var that = this;
    wx.saveImageToPhotosAlbum({
      filePath: that.data.shareImage,
      success(res) {
        that.onHideShare();
        wx.showToast({title: '保存到相册成功',icon: 'success',duration: 2000})
      },fail(res) {
        wx.showToast({title: '保存到相册失败',icon: 'success',duration: 2000})
      }
    })
  },
  //好物圈
  shareBusiness: function (e) {
    var that = this;
    wx.openBusinessView({
      businessType: 'friendGoodsRecommend',
      extraData: {
        product: {
          item_code:this.data.id,
          title:this.data.shop.name,
          image_list:this.data.shop.imgs
        }
      },
      success: function (res) {
        that.onHideShare();
      },
      fail: function (res){
        app.wxAlert('微信版本过低,不支持分享到微信圈子')
      }
    })
  },
  //滚动
  onPageScroll(res) {
    let scrollTop = res.scrollTop;
    this.setData({
      showbar:200 <= scrollTop ? true : false
    })
    wx.setNavigationBarColor({
      frontColor:200 <= scrollTop ? '#000000' :'#ffffff',
      backgroundColor: '#ffffff'
    })
  },
  //返回页面
  onClickLeft() {
    var pages = getCurrentPages();
    if(pages.length == 2){
      wx.navigateBack({
        delta:1,
      })
    }else{
      wx.switchTab({
        url: '/tab/index',
      })
    }
  },
  //返回首页
  onClickHome() {
    wx.switchTab({
      url: '/tab/index',
    })
  },
})